package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectCount;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;

import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Override
    public ResponseResult channeList() {

        Page <WmChannel> page = new Page<>(1,100);
        Page <WmChannel>  wmChannelPage =wmChannelMapper.selectPage(page,null);
        return ResponseResult.okResult(wmChannelPage.getRecords());
    }

    //新建频道
    @Override
    public ResponseResult channelSave(WmChannel wmChannel) {
        if (wmChannel == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //去重
        String name = wmChannel.getName();
        LambdaQueryWrapper<WmChannel >wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName,name);
        WmChannel channel = getOne(wrapper);
        if (channel != null ){
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名已存在");
        }
        //保存

        wmChannel.setCreatedTime(new Date());
        this.save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //查询频道列表
    @Override
    public ResponseResult channelFind(ChannelDto dto) {

        //入参判断
        if (dto == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数不能为空");

        }
        //组装查询条件
        IPage page = new Page(dto.getPage(), dto.getSize());

        String name = dto.getName();
        LambdaQueryWrapper<WmChannel>wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isBlank(name)){
            wrapper.like(WmChannel::getName,name);
        }
        wrapper.orderByDesc(WmChannel::getCreatedTime);
        //构建分页规则

        page = page(page , wrapper);
        //返回

        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult update(WmChannel wmChannel) {

        LambdaQueryWrapper<WmNews> newsWrapper = new LambdaQueryWrapper<>();
        newsWrapper.eq(WmNews::getChannelId, wmChannel.getId());
        newsWrapper.eq(WmNews::getStatus, 9);
        Integer newsCount = wmNewsMapper.selectCount(newsWrapper);
        if (newsCount > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "频道关联了新闻，无法修改");
        }

        boolean updateResult = this.updateById(wmChannel);
        return updateResult ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    //删除频道
    @Override
    public ResponseResult del(Integer id) {

        //查询频道信息
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel == null ){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        //判断频道状态
        Boolean status = wmChannel.getStatus();
        if (status){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道正在启用");
        }
        //检测频道是否有新闻
        LambdaQueryWrapper<WmNews>wrapper = new LambdaQueryWrapper();
        wrapper.eq(WmNews::getId,wmChannel.getId());
        wrapper.eq(WmNews::getStatus,9);
        Integer selectCount = wmNewsMapper.selectCount(wrapper);
        if (selectCount > 0){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //删除
        boolean removeById = this.removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}